package com.xiaode.controller.admin.account;

import com.jfinal.aop.Before;
import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.core.Path;
import com.jfinal.core.paragetter.Para;
import com.jfinal.ext.cors.EnableCORS;
import com.jfinal.kit.PathKit;
import com.jfinal.kit.Ret;
import com.xiaode.common.anotation.Authority;
import com.xiaode.common.anotation.JSONArrayString;
import com.xiaode.common.model.Admin;
import com.xiaode.common.model.dto.StudentPageDTO;
import com.xiaode.util.apidoc.annotation.Api;
import com.xiaode.util.apidoc.annotation.ApiOper;
import com.xiaode.util.apidoc.annotation.ApiPara;

import javax.validation.constraints.Size;
import java.io.File;

/**
 * 账号管理
 */
@Path("/aAccount")
@Api("管理员账号相关服务")
@EnableCORS
public class AccountController extends Controller {

    @Inject
    private AccountService service;

    /**
     * 获取管理员账号列表
     */
    @ApiOper("获取管理员账号列表")
    @Authority(description = "管理员列表", permission = "admin:aAccount:accountPage", permissionParentName = "管理员模块", permissionName = "教师管理", type = 1)
    public void accountPage(
    		@ApiPara(value = "account", notes = "账号") String account,
            @ApiPara(value = "accountSort", notes = "工号排序") String accountSort,
            @ApiPara(value = "name", notes = "姓名") String name,
            @ApiPara(value = "state", notes = "状态1.正常，2.禁用") Integer state,
            @ApiPara(value = "roleId", notes = "角色") Integer roleId,
            @ApiPara(value = "loginDuration", notes = "登录时间") Integer loginDuration,
            @ApiPara(value = "lastLoginTimeSort",notes = "上次登录时间排序")String lastLoginTimeSort,
            @Size @ApiPara(value = "page", notes = "页码") Integer page,
            @Size @ApiPara(value = "limit", notes = "每页数量") Integer limit) throws Exception {
        Admin admin = getAttr("admin");
        Ret ret = service.adminAccountPage(admin, account, name, state, page, limit, roleId, accountSort, loginDuration,lastLoginTimeSort);
        renderJson(ret);
    }

    /**
     * 查询届别
     */
    @ApiOper("查询届别")
    @Authority(description = "查询届别", permission = "admin:aAccount:getSchoolYear", permissionParentName = "管理员模块", permissionName = "学生管理",type = 1)
    public void getSchoolYear() {
        Admin admin = getAttr("admin");
        Ret ret = service.getSchoolYear(admin);
        renderJson(ret);
    }


    /**
     * 编辑管理员账号
     */
    @ApiOper("编辑管理员账号")
    @Before(AccountFileValidator.class)
    @Authority(description = "编辑管理员账号", permission = "admin:aAccount:editAdmin", permissionParentName = "管理员模块", permissionName = "教师管理",type = 2)
    public void editAdmin(
	          @Size @ApiPara(value = "uAdminId", notes = "管理员id") Integer uAdminId,
	          @Size(min = 6,max = 12) @ApiPara(value = "account", notes = "账号") String account,
	          @ApiPara(value = "state", notes = "状态1.正常，2.禁用") Integer state,
	          @JSONArrayString @ApiPara(value = "roleIds", notes = "角色id数组字符串") String roleIds) throws Exception {
        Admin admin = getAttr("admin");
        Ret ret = service.editAdmin(admin, uAdminId, account, state, roleIds);
        renderJson(ret);
    }

    /**
     * 禁用启用管理员账号
     */
    @ApiOper("禁用启用管理员账号")
    @Authority(description = "编辑管理员账号", permission = "admin:aAccount:changeAdminState", permissionParentName = "管理员模块", permissionName = "教师管理",type = 2)
    public void changeAdminState(
    		@Size @ApiPara(value = "state", notes = "管理员状态") Integer state,
            @JSONArrayString @ApiPara(value = "adminIds", notes = "管理员id[id]") String adminIds) throws Exception {
        Ret ret = service.adminState(adminIds, state);
        renderJson(ret);
    }

    /**
     * 删除管理员账号
     */
//    @ApiOper("删除管理员账号")
//    @Authority(description = "删除管理员账号", permission = "admin:aAccount:deleAdmin", permissionParentName = "管理员模块", permissionName = "教师管理",type = 2)
//    public void deleAdmin(@JSONArrayString @ApiPara(value = "adminIds", notes = "管理员id[id]") String adminIds) throws Exception {
//        Admin admin = getAttr("admin");
//        Ret ret = service.deleAdmin(admin, adminIds);
//        renderJson(ret);
//    }

    /**
     * 添加管理账号
     */
//    @ApiOper("添加管理账号")
//    @Authority(description = "添加管理账号", permission = "admin:aAccount:addAdmin", permissionParentName = "管理员模块", permissionName = "教师管理",type = 2)
//    public void addAdmin(
//             @Size(min = 6,max = 12) @ApiPara(value = "account", notes = "账号") String account,
//             @NotBlank @ApiPara(value = "name", notes = "名称") String name,
//             @JSONArrayString @ApiPara(value = "roleIds", notes = "角色id数组字符串[]") String roleIds) throws Exception {
//        Ret ret = service.addAdmin(account, name, roleIds);
//        renderJson(ret);
//    }

    /**
     * 获取学生账号列表
     */
    @ApiOper("学生账号列表")
    @Authority(description = "学生账号列表", permission = "admin:sAccount:studentPage", permissionParentName = "管理员模块", permissionName = "学生管理",type = 1)
    public void studentPage(
            @ApiPara(value = "model", notes = "参数封装") @Para("") StudentPageDTO model,
            @Size @ApiPara(value = "page", notes = "页码") Integer page,
            @Size @ApiPara(value = "limit", notes = "每页数量") Integer limit) throws Exception {
        Admin admin = getAttr("admin");
        Ret ret = service.studentPage(admin, model, page, limit);
        renderJson(ret);
    }

    /**
     * 查询届别,班级
     */
    @ApiOper("查询届别班级")
    @Authority(description = "查询届别班级", permission = "admin:sAccount:findClassInfo", permissionParentName = "管理员模块", permissionName = "学生管理",type = 1)
    public void findClassInfo() {
        Admin admin = getAttr("admin");
        Ret ret = service.findClassInfo(admin);
        renderJson(ret);
    }

    /**
     * 编辑学生账号
     */
    @ApiOper("编辑学生账号")
    @Before(AccountFileValidator.class)
    @Authority(description = "编辑学生账号", permission = "admin:sAccount:editStudent", permissionParentName = "管理员模块", permissionName = "学生管理",type = 2)
    public void editStudent(
    		@Size @ApiPara(value = "studentId", notes = "学生id") Integer studentId,
            @ApiPara(value = "state", notes = "状态1.正常，2.禁用") Integer state,
            @ApiPara(value = "teacherId", notes = "教师id") Integer teacherId) throws Exception {
        Admin admin = getAttr("admin");
        Ret ret = service.editStudent(admin,studentId,  state, teacherId);
        renderJson(ret);
    }

    /**
     * 删除学生账号
     */
//    @ApiOper("删除学生账号")
//    @Authority(description = "删除学生账号", permission = "admin:sAccount:deleStudent", permissionParentName = "管理员模块", permissionName = "学生管理",type = 2)
//    public void deleStudent(@Size @ApiPara(value = "studentIds", notes = "学生id") String studentIds) throws Exception {
//        Admin admin = getAttr("admin");
//        Ret ret = service.deleStudent(admin,studentIds);
//        renderJson(ret);
//    }

    /**
     * 重置学生密码
     */
    @ApiOper("重置学生密码")
    @Authority(description = "重置学生密码", permission = "admin:sAccount:resetStudentPass", permissionParentName = "管理员模块", permissionName = "学生管理",type = 2)
    public void resetStudentPass(@Size @ApiPara(value = "studentId", notes = "学生id") Integer studentId) throws Exception {
        Admin admin = getAttr("admin");
        Ret ret = service.resetStudentPass(admin,studentId);
        renderJson(ret);
    }

    /**
     * 重置admin密码
     */
    @ApiOper("重置admin密码")
    @Authority(description = "重置admin密码", permission = "admin:aAccount:resetAdminPass", permissionParentName = "管理员模块", permissionName = "教师管理",type = 2)
    public void resetAdminPass(@Size @ApiPara(value = "adminId", notes = "账号id") Integer adminId) throws Exception {
        Ret ret = service.resetAdminPass(adminId);
        renderJson(ret);
    }

    /**
     * 添加学生账号
     */
//    @ApiOper("添加学生账号")
//    @Authority(description = "添加学生账号", permission = "admin:sAccount:addStudent", permissionParentName = "管理员模块", permissionName = "学生管理",type = 2)
//    public void addStudent(@Size(min = 1, max = 5) @ApiPara(value = "number", notes = "学生编号") String number,
//                           @Size(min = 10, max = 20) @ApiPara(value = "account", notes = "账号") String account,
//                           @NotBlank @ApiPara(value = "name", notes = "名称") String name,
//                           @Size @ApiPara(value = "schoolYear", notes = "学年") String schoolYear,
//                           @Size @ApiPara(value = "clasId", notes = "班级id") Integer clasId,
//                           @Size @ApiPara(value = "sex", notes = "性别") Integer sex,
//                           @Size @ApiPara(value = "isMainland", notes = "是否内高学生") Integer isMainland,
//                           @ApiPara(value = "teacherId", notes = "教师id") Integer teacherId
//    ) throws Exception {
//        Admin admin = getAttr("admin");
//        Ret ret = service.addStudent(admin, number, account, name, schoolYear,clasId, isMainland, sex, teacherId);
//        renderJson(ret);
//    }

    /**
     * 批量禁用/启用
     */
//    @ApiOper("批量禁用/启用")
//    @Authority(description = "批量禁用/启用", permission = "admin:sAccount:batchUpdate", permissionParentName = "管理员模块", permissionName = "学生管理",type = 2)
//    public void batchUpdate(@Size @ApiPara(value = "studentIds", notes = "学生ids") String studentIds,
//                            @Size @ApiPara(value = "state", notes = "状态(1.正常 2.禁用)") Integer state) throws Exception {
//        Admin admin = getAttr("admin");
//        Ret ret = service.batchUpdate(admin,studentIds, state);
//        renderJson(ret);
//    }

    /**
     * 家长查询
     */
    @ApiOper("家长查询")
    @Authority(description = "家长查询", permission = "admin:pAccount:parentPage", permissionParentName = "管理员模块", permissionName = "家长管理",type = 1)
    public void parentPage(@ApiPara(value = "studentName", notes = "学生姓名") String studentName,
                           @ApiPara(value = "schoolYear", notes = "届别") String schoolYear,
                           @ApiPara(value = "lastLoginTimeSort", notes = "最后登录时间排序") String lastLoginSort,
                           @ApiPara(value = "state", notes = "状态") Integer state,
                           @ApiPara(value = "account", notes = "学籍号") String account,
                           @ApiPara(value = "role", notes = "角色") String role,
                           @ApiPara(value = "clasId", notes = "班级") Integer clasId,
                           @Size @ApiPara(value = "page", notes = "当前页") Integer page,
                           @Size @ApiPara(value = "limit", notes = "每页大小") Integer limit) throws Exception {
        Admin admin = getAttr("admin");
        Ret ret = service.parentPage(admin, studentName, schoolYear, lastLoginSort, state, clasId, account, role, page, limit);
        renderJson(ret);
    }

    /**
     * 批量启用/禁用
     */
    @ApiOper("批量启用/禁用家长")
    @Authority(description = "启用/禁用家长", permission = "admin:pAccount:changeParentState", permissionParentName = "管理员模块", permissionName = "家长管理",type = 2)
    public void changeParentState(@Size @ApiPara(value = "state", notes = "状态") Integer state,
                                  @Size @ApiPara(value = "parentIds", notes = "家长ids[id]") String parentIds) {
        Admin admin = getAttr("admin");
        Ret ret = service.updateStateParent(admin, state, parentIds);
        renderJson(ret);
    }

    /**
     * 批量删除家长
     */
    @ApiOper("批量删除家长")
    @Authority(description = "批量删除家长", permission = "admin:pAccount:delParent", permissionParentName = "管理员模块", permissionName = "家长管理",type = 2)
    public void delParent(@Size @ApiPara(value = "parentIds", notes = "家长ids[id]") String parentIds) {
        Admin admin = getAttr("admin");
        Ret ret = service.delParent(admin, parentIds);
        renderJson(ret);
    }


    /**
     * 导入学生信息
     */
//    @ApiOper("导入学生名单")
//    @Before(AccountFileValidator.class)
//    @Authority(description = "导入学生名单", permission = "admin:sAccount:importStudent", permissionParentName = "管理员模块", permissionName = "学生管理",type = 2)
//    public void importStudent(@ApiPara(value = "isUpdateExist", notes = "是否更新已存在学生数据") Boolean isUpdateExist) throws Exception {
//        UploadFile exl = getFile();
//        isUpdateExist = getParaToBoolean("isUpdateExist");
//        Ret ret = service.importStudent(exl, isUpdateExist);
//        renderJson(ret);
//    }

    /**
     * 下载学生模板
     */
    @ApiOper("下载学生模板")
    @Authority(description = "下载学生模板", permission = "admin:sAccount:exportStudentModel", permissionParentName = "管理员模块", permissionName = "学生管理",type = 1)
    public void exportStudentModel()throws Exception{
        String path = PathKit.getWebRootPath() + "/download/studentInfo_model.xlsx";
        renderFile(new File(path));
    }

    /**
     * 导入教师名单
     */
//    @ApiOper("导入教师名单")
//    @Before(AccountFileValidator.class)
//    @Authority(description = "导入教师名单", permission = "admin:aAccount:importTeacher", permissionParentName = "管理员模块", permissionName = "教师管理",type = 2)
//    public void importTeacher(@ApiPara(value = "isUpdateExist", notes = "是否更新已存在教师数据") Boolean isUpdateExist) throws Exception {
//        UploadFile teacherExl = getFile();
//        isUpdateExist = getParaToBoolean("isUpdateExist");
//        Ret ret = service.importTeacher(teacherExl,isUpdateExist);
//        renderJson(ret);
//    }

    /**
     * 下载教师模板
     * @throws Exception
     */
    @ApiOper("下载教师模板")
    @Authority(description = "下载教师模板", permission = "admin:aAccount:exportTeacherModel", permissionParentName = "管理员模块", permissionName = "教师管理",type = 1)
    public void exportTeacherModel()throws Exception{
        String path = PathKit.getWebRootPath() + "/download/teacher_model.xlsx";
        renderFile(new File(path));
    }



    @ApiOper("导出学生登录信息")
    @Authority(description = "导出学生登录信息", permission = "admin:sAccount:exportStuLoginInfo", permissionParentName = "管理员模块", permissionName = "学生管理",type = 1)
    public void exportStuLoginInfo() throws Exception {
        String path = service.exportStuLoginInfo();
        renderFile(new File(path), "学生信息表.xlsx");
    }

    @ApiOper("导出教师信息")
    @Authority(description = "导出教师登录信息", permission = "admin:aAccount:exportTeaLoginInfo", permissionParentName = "管理员模块", permissionName = "教师管理",type = 1)
    public void exportTeaInfo() throws Exception {
        String path = service.exportTeaInfo();
        renderFile(new File(path));
    }

    @ApiOper("导出家长信息")
    @Authority(description = "导出家长信息", permission = "admin:pAccount:exportParentInfo", permissionParentName = "管理员模块", permissionName = "家长管理",type = 1)
    public void exportParentInfo() throws Exception {
        String path = service.exportParentInfo();
        renderFile(new File(path), "教师信息表");
    }

    
    
    /**
     * 添加外部账号
     * TODO
     */
    @ApiOper("添加外部账号")
    @Authority(description = "添加外部账号", permission = "admin:pAccount:exportParentInfo", permissionParentName = "管理员模块", permissionName = "外部账号管理",type = 1)
    public void addExternalInfo() throws Exception {
    	Ret ret = service.addExternalInfo();
        renderJson(ret);
    }
    
    
    

}

